package com.yangye.picommon.core.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yangye.picommon.core.dao.BaseMapper;
import com.yangye.picommon.core.entity.BaseEntity;
import com.yangye.picommon.core.service.BaseService;
import com.yangye.picommon.model.page.PageRequest;
import com.yangye.picommon.model.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 通用service实现类
 * @author yangye
 * @date 2019/3/29 0029
 */
public abstract class BaseServiceImpl<D extends BaseMapper<E>, E extends BaseEntity> implements BaseService<E> {
    @Autowired
    protected D dao;

    @Override
    public E get(Long id) {
        return dao.selectByPrimaryKey(id);
    }

    @Override
    public E get(E entity) {
        return dao.selectOne(entity);
    }

    @Override
    public List<E> findAll(E entity) {
        return dao.select(entity);
    }

    @Override
    public PageResult<E> findPage(E entity, PageRequest pageRequest) {
        Page<E> page = PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        dao.select(entity);
        return new PageResult<E>(page);
    }

    @Override
    public E add(E entity) {
        dao.insertSelective(entity);
        return entity;
    }

    @Override
    public int batchAdd(List<E> list) {
        return dao.insertList(list);
    }

    @Override
    public E update(E entity) {
        dao.updateByPrimaryKeySelective(entity);
        return entity;
    }

    @Override
    public void remove(E entity) {
        dao.delete(entity);
    }

    @Override
    public void remove(Long id) {
        dao.deleteByPrimaryKey(id);
    }
}
